package demo.auth.config;

import demo.common.auth.utils.RsaUtils;
import demo.common.exception.SnowException;
import demo.common.exception.SnowExceptionEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.security.PrivateKey;
import java.security.PublicKey;

@Data
@Slf4j
@Component
@ConfigurationProperties(prefix = "demo.jwt")
public class JwtPropertiesConfig implements InitializingBean {
    // 读取配置的公钥和私钥地址
    private String pubKeyPath;
    private String priKeyPath;

    // 通过RsaUtils工具类，获取公钥和私钥
    private PublicKey publicKey;
    private PrivateKey privateKey;

    private UserTokenProperties user = new UserTokenProperties();

    /**
     * 当对象属性赋值后要处理的事情
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            // 获取公钥和私钥
            publicKey = RsaUtils.getPublicKey(pubKeyPath);
            privateKey = RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            log.error("【授权中心】读取公钥和私钥失败");
            e.printStackTrace();
//            throw new SnowException(SnowExceptionEnum.INVALID_PARAM_ERROR);
        }
    }

    @Data
    public class UserTokenProperties {
        // token过期时长
        private int expire;

        // 存放Token的cookie名称
        public String cookieName;

        // 存放token的cookie的domain
        public String cookieDomain;

        // 当前时间距离token过期时间不足15分钟，重新生成token
        private int minRefreshInterval;
    }
}
